/**
 * Created by root on 19-11-22.
 * 使用透视矩阵
 */

#ifndef OPENGLESPRO_GAME_STEP_5_H
#define OPENGLESPRO_GAME_STEP_5_H

#define VERTEX_ATTR_INDEX_V5_POSITION 0
#define VERTEX_ATTR_INDEX_V5_COLOR 1
#define VERTEX_VEC_COUNT_V5_PER_POSITION 2
#define VERTEX_VEC_COUNT_V5_PER_COLOR 3
#define STRIDE_BYTES_V5 sizeof(GLfloat)*(VERTEX_VEC_COUNT_V5_PER_POSITION+VERTEX_VEC_COUNT_V5_PER_COLOR)

void OnSurfaceCreatedV5();

void OnSurfaceCreatedV5() {}

void OnSurfaceChangedV5();

void OnSurfaceChangedV5() {}

void OnDrawFrameV5();

void OnDrawFrameV5() {
    GLfloat vertexPointers[] = {
            // Order of coordinates: X, Y, R, G, B

            // Triangle Fan
            0, 0, 1, 1, 1,
            -0.5, -0.8, 0.7, 0.7, 0.7,
            0.5, -0.8, 0.7, 0.7, 0.7,
            0.5, 0.8, 0.7, 0.7, 0.7,
            -0.5, 0.8, 0.7, 0.7, 0.7,
            -0.5, -0.8, 0.7, 0.7, 0.7,

            // Line
            -0.5, 0, 1, 0, 0,
            0.5, 0, 1, 0, 0,

            // Mallets
            0, -0.4, 0, 0, 1,
            0, 0.4, 1, 0, 0
    };
    glEnableVertexAttribArray(VERTEX_ATTR_INDEX_V5_COLOR);
    glEnableVertexAttribArray(VERTEX_ATTR_INDEX_V5_POSITION);
    glLineWidth(3);
    glVertexAttribPointer(VERTEX_ATTR_INDEX_V5_POSITION, VERTEX_VEC_COUNT_V5_PER_POSITION, GL_FLOAT,
                          GL_FALSE, STRIDE_BYTES_V5, vertexPointers);
    glVertexAttribPointer(VERTEX_ATTR_INDEX_V5_COLOR, VERTEX_VEC_COUNT_V5_PER_COLOR, GL_FLOAT,
                          GL_FALSE, STRIDE_BYTES_V5,
                          &vertexPointers[VERTEX_VEC_COUNT_V5_PER_POSITION]);

    float transformMatrix[] = {
            1, 0, 0, 0,
            0, 1, 0, 0,
            0, 0, 1, -2.5,
            0, 0, 0, 1
    };//原本是沿着z轴平移-2个单位，但考虑到旋转，会导致距视椎体近平面的部分被截断，故而平移增至-2.5个单位
    glUniformMatrix4fv(glGetUniformLocation(device.program, "transformMatrix"), 1, GL_FALSE,
                       transformMatrix);//由于下面的视椎体z分量范围是-1～~10，所以这里将所有顶点坐标沿着z轴平移至-2，保证处于视椎体范围内

    int w = device.width;
    int h = device.height;
    glUniform1f(glGetUniformLocation(device.program, "aspect"),
                w > h ? (float) h / (float) w : (float) w / (float) h);
    glUniform1f(glGetUniformLocation(device.program, "far"), 10);
    glUniform1f(glGetUniformLocation(device.program, "near"), 1);
    glUniform1f(glGetUniformLocation(device.program, "scanRad"), (float) (PI / 2));//90度

    glDrawArrays(GL_TRIANGLE_FAN, 0, 6);
    glDrawArrays(GL_LINES, 6, 2);
    glDrawArrays(GL_POINTS, 8, 1);
    glDrawArrays(GL_POINTS, 9, 1);
}

#endif //OPENGLESPRO_GAME_STEP_5_H
